package io.wen.scala

object BeerCounter {
  def main(args: Array[String]): Unit = {
    val money: Int = 100
    val price: Int = 2
    println(s"现金：$money 元，啤酒价格：$price 元")
    // 用钱一次买完啤酒
    val buyCount: Int = money / price

    val total = buyCount + changeByBottleAndCap(buyCount, buyCount)
    println(s"喝啤酒总数: $total 瓶")
  }

  /**
    * 使用酒瓶和瓶盖换啤酒
    * @param bottle 酒瓶 3换1
    * @param cap 瓶盖 5换1
    * @return 换到的啤酒数
    */
  def changeByBottleAndCap(bottle: Int, cap: Int): Int = {
    val canChange: Boolean = bottle >= 3 && cap >= 5
    if (!canChange) {
      return 0
    }

    val count = (bottle / 3) + (cap / 5)  // 此轮更换的啤酒数
    val remainBottle = bottle % 3 + count // 此轮更换后剩余酒瓶
    val remainCap = cap % 5 + count       // 此轮更换后剩余瓶盖

    count + changeByBottleAndCap(remainBottle, remainCap)
  }
}